<template>
  <div>
    <input class="uploadFile" type="file" ref="fileInput" @change="uploadFile($event.target)" accept=".jpg,.png">
    <div class="upload_pictures">
      <div class="upload_btns" @click="uploadFiless">
        <i class="el-icon-plus add_class"></i>
        <div class="upload_txt">上传图片</div>
      </div>
      <div class="view_img" v-for="(item,index) in fileLists" :key="index">
        <el-image
          class="view_img_url"
          :src="item"
          :preview-src-list="fileLists">
        </el-image>
        <img @click="delImg(index)" class="close_img" src="@/assets/images/close_img.png" alt="加载失败">
      </div>
    </div>
  </div>
</template>

<script>
import { fammterString,uploadOSS } from "@/api/upload";

export default {
  props: {
    maxImageSize: {
      type: Number,
      default: 5 // 默认最大图片大小为5MB
    },
    maxVideoSize: {
      type: Number,
      default: 50 // 默认最大视频大小为50MB
    },
    type:{
      type: [Number,String],
      default: '' // 默认最大图片大小为5MB
    },
    echoFile: {
      type: Array,
      default: [] // 回显文件列表
    }
  },
  components:{

  },
  watch: {
    echoFile: {
      handler(val) {
        console.log(val,'fileLists')
        this.fileLists=val
      }
    }
  },
  created() {

  },
  data(){
    return{
      fileLists:[]
    }
  },
  methods: {
    lookFiles(url){
      window.open(url)
    },
    uploadFiless(){
      const fileInput = this.$refs.fileInput
      fileInput.click()
    },
    uploadFile(event) {
      const file = this.$refs.fileInput.files[0];
      if (!file) {
        return;
      }
      const fileType = file.type.split('/')[0];
      const fileSize = file.size / 1024 / 1024; // 转换为MB
      if (fileType === 'image' && fileSize > this.maxImageSize) {
        this.$message.warning(`图片大小不能超过${this.maxImageSize}MB`);
        return;
      }
      if (fileType === 'video' && fileSize > this.maxVideoSize) {
        this.$message.warning(`视频大小不能超过${this.maxVideoSize}MB`);
        return;
      }
      this.uploadFiles(event,file)
      // 进行上传操作
    },
    async uploadFiles(event,file){
      let res
      try {
        let path=fammterString(this.type,localStorage.getItem('uuid'))
        res=await uploadOSS(file,path)
      }catch (e){
        console.error(e)
      }
      if (res){
        this.fileLists.push(res[0])
      }
      this.$emit('uploadFiles',this.fileLists)
      //重置input
      event.value=''
    },
    delImg(index){
      this.fileLists.splice(index,1)
      this.$emit('uploadFiles',this.fileLists)
    },
  }
};
</script>
<style lang="scss" scoped>
.uploadFile{
  display: none;
}
.upload_pictures{
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
}
.upload_btns{
  width: 148px;
  height: 148px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: #ffffff;
  cursor: pointer;
  border: 1px dashed #c0ccda;
  border-radius: 8px;
}
.add_class{
  font-size: 18px;
  font-weight: bold;
  color: #999999;
}
.upload_txt{
  margin-top: 8px;
  font-size: 10px;
  font-weight: 400;
  color: rgba(0,0,0,0.65);
  user-select: none;
}
.view_img{
  width: auto !important;
  height: 148px;
  margin-left: 10px;
  margin-bottom: 10px;
  position: relative;
  border: 1px dashed #c0ccda;
  border-radius: 8px;
}
.view_img_url{
  width: auto !important;
  height: 100%;
}
.video_box{
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f5f5;
  cursor: pointer;
}
.other_box{
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: #f5f5f5;
  cursor: pointer;
  .el-icon-document{
    font-size: 18px;
    color: #565656;
  }
  .other_text{
    margin-top: 10px;
    font-size: 12px;
    color: #565656;
  }
}
.video_img{
  width: 24px;
  height: 24px;
  object-fit: cover;
  position: absolute;
}
.close_img{
  width: 16px;
  height: 16px;
  position: absolute;
  top: -4px;
  right: -4px;
  cursor: pointer;
}
</style>
